package org.example.rubbish.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.rubbish.common.PageResult;
import org.example.rubbish.entity.ForumPost;
import org.example.rubbish.result.Result;
import org.example.rubbish.service.ForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/admin/forum")
public class ForumController {

    @Autowired
    private ForumService forumService;

    @GetMapping("/posts")
    public Result<PageResult<ForumPost>> getForumPosts(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String author,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String province,
            @RequestParam(required = false) String city,
            @RequestParam(required = false) String district) {

        PageResult<ForumPost> result = forumService.getForumPosts(
                page, size, title, author, status, province, city, district);
        return Result.success(result);
    }

    @GetMapping("/posts/bounds-search")
    public Result<PageResult<ForumPost>> searchPostsByBounds(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam Double swLat,  // 西南角纬度
            @RequestParam Double swLng,  // 西南角经度
            @RequestParam Double neLat,  // 东北角纬度
            @RequestParam Double neLng,  // 东北角经度
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String author,
            @RequestParam(required = false) String status) {



        PageResult<ForumPost> result = forumService.searchPostsByBounds(
                page, size, swLat, swLng, neLat, neLng, title, author, status);
        return Result.success(result);
    }

    @GetMapping("/national-distribution")
    public Result<Map<String, Object>> getNationalPostDistribution() {
        Map<String, Object> distribution = forumService.getNationalPostDistribution();
        return Result.success(distribution);
    }

    @GetMapping("/region-stats-report")
    public Result<List<Map<String, Object>>> getRegionStatsReport(
            @RequestParam(defaultValue = "province") String level) {

        List<Map<String, Object>> stats = forumService.getRegionStatsReport(level);
        return Result.success(stats);
    }

    @GetMapping("/posts/heatmap")
    public Result<List<Map<String, Object>>> getForumHeatmapData(
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String province,
            @RequestParam(required = false) String city) {

        List<Map<String, Object>> heatmapData = forumService.getForumHeatmapData(status, province, city);
        return Result.success(heatmapData);
    }


    @PostMapping("/posts")
    public Result<ForumPost> createForumPost(@RequestBody ForumPost forumPost) {
        ForumPost created = forumService.createForumPost(forumPost);
        return Result.success(created);
    }

    @PutMapping("/posts/{id}")
    public Result<ForumPost> updateForumPost(@PathVariable Long id, @RequestBody ForumPost forumPost) {
        forumPost.setId(id);
        ForumPost updated = forumService.updateForumPost(forumPost);
        return Result.success(updated);
    }

    @DeleteMapping("/posts/{id}")
    public Result<Void> deleteForumPost(@PathVariable Long id) {
        forumService.deleteForumPost(id);
        return Result.success();
    }

    @PostMapping("/posts/batch-delete")
    public Result<Void> batchDeleteForumPosts(@RequestBody List<Long> ids) {
        forumService.batchDeleteForumPosts(ids);
        return Result.success();
    }

    @PutMapping("/posts/{id}/status")
    public Result<Void> updateForumPostStatus(@PathVariable Long id, @RequestParam String status) {
        forumService.updateForumPostStatus(id, status);
        return Result.success();
    }

    @GetMapping("/stats")
    public Result<Object> getForumStats() {
        Object stats = forumService.getForumStats();
        return Result.success(stats);
    }
}